package cn.javabb.admin.controller.biz;

import cn.javabb.common.model.ResponseModel;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.blog.BlogTypeDO;
import cn.javabb.core.model.StatusEnum;
import cn.javabb.core.service.blog.BlogTypeService;
import cn.javabb.log.action.DeleteAction;
import cn.javabb.log.action.StateAction;
import cn.javabb.log.annotation.Log;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/12/05 16:47
 */
@Controller
@RequestMapping("/admin/blogType")
public class BlogTypeAdminController extends BaseController {
    @Autowired
    private BlogTypeService blogTypeService;

    /**
     * 列表页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String toListPage(Model model) {
        return "admin/biz/blog/blogType";
    }
    /**
     * 保存页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/save")
    public String toSavePage(Model model) {
        return "admin/biz/blog/blogTypeSave";
    }
    /**
     * 查询列表
     */
    @ResponseBody
    @GetMapping("/tree")
    public Object list() {
        List<BlogTypeDO> allType = blogTypeService.queryAll();
        return ResponseModel.Success(allType);
    }
    /**
     * 查询列表
     */
    @ResponseBody
    @GetMapping("/treeSelect")
    public Object treeSelect() {
        List<BlogTypeDO> allType = blogTypeService.listBlogTypes();
        return ResponseModel.Success(allType);
    }
    /**
     * 更新
     * @return
     */
    @ResponseBody
    @PostMapping("/")
    public Object saveOrUpdate(BlogTypeDO type){
        int n = 0;
        if(null == type.getId()){
            if(null == type.getState()){
                type.setState(StatusEnum.NORMAL.getCode());
            }
            n = blogTypeService.insertSelective(type);
        }else{
            n = blogTypeService.updateSelective(type);
        }
        if(n>0){
            return ResponseModel.Success("保存成功");
        }
        return ResponseModel.Failure("保存失败");

    }

    /**
     * 更新状态
     * @param ids
     * @param state
     * @return
     */
    @RequiresPermissions("blogType:edit")
    @ResponseBody
    @PostMapping("/updateState")
    @Log(name="博客类别状态",action = StateAction.class)
    public Object updateState(String ids,Integer state){
        if(blogTypeService.updateState(ids,state)){
            return ResponseModel.Success("状态更新成功");
        }
        return ResponseModel.Failure("状态更新失败");
    }
    /**
     * 删除
     * @param ids
     * @return
     */
    @RequiresPermissions("blogType:del")
    @ResponseBody
    @DeleteMapping("/{ids}")
    @Log(name = "博客类别删除",action = DeleteAction.class)
    public Object delete(@PathVariable String ids){
        if (blogTypeService.delete(ids)) {
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }


}
